
package com.updesignsz.api.controller;

import com.updesignsz.api.service.NewsService;
import com.updesignsz.api.util.ParamsUtil;
import com.updesignsz.api.util.ResponseUtil;
import com.updesignsz.commom.util.PageRequest;
import com.updesignsz.commom.util.ResultVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 新闻
 * Creation date 2020-8-8 14:56:12
 */
@RestController
@RequestMapping(value = "/api/news", method = RequestMethod.POST)
public class NewsController {

	@Autowired
	private NewsService newsService;
	
	
	/**
	 * 新闻列表
	 */
	@RequestMapping(value = "/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultVo vo = new ResultVo();
		try {
			JSONObject json = ResponseUtil.getRequestAsJSONObject(request);
			PageRequest page = new PageRequest();
			page.setPageNumber(json.optInt("pageNumber", ParamsUtil.pageNumber));
			page.setPageSize(json.optInt("pageSize", ParamsUtil.pageSize));
			page.setFilters(json);
			vo.setData(newsService.findByPage(page).getList());
		} catch (Exception e) {
			e.printStackTrace();
		}
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 新闻详情
	 */
	@RequestMapping("/detail")
	public void detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultVo vo = new ResultVo();
		try {
			JSONObject json = ResponseUtil.getRequestAsJSONObject(request);
			vo.setData(newsService.getFrontOrAfterID(json));
		} catch (Exception e) {
			vo.setResultVo(-1, "查询新闻详情失败，系统异常！");
			e.printStackTrace();
		}
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}
}